package com.ug_project.dict.engine.wordtable

import org.junit.Test

import org.junit.Assert.*
import java.nio.channels.FileChannel
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption

class WordRowTest {

    fun provideWordTable(autoClean: Boolean = true, action: (table: WordTable) -> Unit) {
        val file = Paths.get("temp_wordrow.wpt")
        if (autoClean) Files.deleteIfExists(file)
        if (Files.notExists(file)) Files.createFile(file)
        val channel = FileChannel.open(file, StandardOpenOption.READ, StandardOpenOption.WRITE)
        val table = WordTable(channel)
        action(table)
        channel.close()
    }

    @Test
    fun add() {
        provideWordTable { table ->
            var unicode = 'a'.toInt()
            var row = table[unicode]
            if (row == null) {
                row = WordRow(table, table.channel.size().toInt())
                table[unicode] = row
            }

            assertEquals(0, row.layerCount)

            row.add(97, 10)

            assertEquals(11, row.layerCount)
            assertEquals(97, row.pointers[10])









            unicode = 'b'.toInt()
            row = table[unicode]
            if (row == null) {
                row = WordRow(table, table.channel.size().toInt())
                table[unicode] = row
            }

            assertEquals(0, row.layerCount)

            row.add(98, 10)

            assertEquals(11, row.layerCount)
            assertEquals(98, row.pointers[10])

            table.write()

            println("ok")
        }
    }

    @Test
    fun raadWrite() {
        provideWordTable { tableSource ->
            val unicode = 'a'.toInt()
            val row = WordRow(tableSource, 0)
            tableSource[unicode] = row
        }
    }
}
